import time
from common.Utils import SHPDriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#测试商城系统分类页测试
class CategoryPage:
    def __init__(self):
        self.url = "http://49.235.61.184/"
        self.driver = SHPDriver.driver
        self.driver.get(self.url)

    def categoryTest1(self):
        # 1.登录
        SHPDriver.Auto_Login()
        # 初始化页面：打开分类页
        self.driver.find_element(By.XPATH, '//*[@id="goods-category"]/div/a/div/span').click()
        # 检查页面核心元素（仅验证元素存在，不做额外操作）
        # 全部分类按钮
        self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > a > div > span")
        # 分类商品类型栏
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div")
        # 类型栏子类栏
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul")
        # 添加购物车按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul > li:nth-child(1) > div > div > div.bottom-content.am-text-truncate > i")
        # 分页框
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul")
        # 截图
        SHPDriver.GetScreenShot("category_check_elements")
        # 功能测试
        # 展示所有商品
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li.am-text-break.active"
        ).click()

        # 在手机通讯 选择手机 查找符合要求商品
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li.am-text-break.active"
        ).click()
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div[2]/div/div/ul/li[2]").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul > li:nth-child(1) > div > a > img").click()
        time.sleep(1)
        SHPDriver.GetScreenShot("mobile_category_search")

        # 选择时尚服饰 选择女装 选择连衣裙 查找符合要求商品
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div > div > div > ul > li:nth-child(2) > div > a"
        ).click()
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li:nth-child(2)"
        ).click()
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-title.am-scrollable-horizontal > div > a:nth-child(8)"
        ).click()
        time.sleep(1)
        SHPDriver.GetScreenShot("women_dress_search")

        # 选择男装 选择休闲裤 查找符合要求商品
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li:nth-child(3)"
        ).click()
        self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-title.am-scrollable-horizontal > div > a:nth-child(18)"
        ).click()
        time.sleep(1)
        SHPDriver.GetScreenShot("men_casual_pants_search")

        # 选择名品潮包 潮流女包 手提包 查找符合要求商品
        self.driver.find_element(By.XPATH,
                                 "/html/body/div[1]/div[1]/div[5]/div/div[1]/div/div/div/div/ul/li[3]/div").click()
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div[2]/div/div/ul/li[6]").click()
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div[3]/div/div[1]/div/a[6]").click()
        time.sleep(3)
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div[3]/div/div[2]/div/ul/li/div/a/img").click()
        time.sleep(1)
        SHPDriver.GetScreenShot("women_handbag_search")

        # 直接在分类页面将商品添加购物车
        # 获取添加前购物车数量
        cart_count_before = self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div"
        ).text
        # 点击添加购物车按钮
        self.driver.find_element(
            By.XPATH,
            "/html/body/div[1]/div[1]/div[5]/div/div[3]/div/div[2]/div/ul/li[1]/div/div/div[2]/i"
        ).click()
        time.sleep(1)
        SHPDriver.GetScreenShot("add_to_cart")

        # 获取添加后购物车数量
        cart_count_after = self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div"
        ).text
        # 断言加入购物车后数量变化
        assert cart_count_before != cart_count_after, "购物车数量未变化，添加商品失败"

        # 分页框功能
        # 设置每页两条（通过键盘回车操作）
        page_size_input = self.driver.find_element(
            By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > span.current-page-input > input"
        )
        actions = ActionChains(self.driver)
        actions.double_click(page_size_input).send_keys(Keys.DELETE).perform()
        page_size_input.send_keys("2", Keys.ENTER)
        time.sleep(1)

        # 点击全部分类按钮
        self.driver.find_element(
            By.CSS_SELECTOR,
            "#goods-category > div > a > div"
        ).click()
        time.sleep(1)
# 执行测试
if __name__ == "__main__":
    category_page = CategoryPage()
    category_page.categoryTest1()
    # 执行完成后保留浏览器5秒再关闭
    time.sleep(5)
    SHPDriver.quit_driver()